package com.example.wyh.intelcheckbash;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

public class BluetoothChatService {
    private static final boolean D = true;
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    private static final String NAME = "BluetoothCom";
    public static final int STATE_CONNECTED = 3;
    public static final int STATE_CONNECTING = 2;
    public static final int STATE_LISTEN = 1;
    public static final int STATE_NONE = 0;
    private static final String TAG = "BluetoothChatService";
    private AcceptThread mAcceptThread;
    private final BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
    private ConnectThread mConnectThread;
    private ConnectedThread mConnectedThread;
    private final Handler mHandler;
    private int mState = 0;

    public BluetoothChatService(Context paramContext, Handler paramHandler) {
        this.mHandler = paramHandler;
    }

    private void connectionFailed() {
        setState(1);
        Message localMessage = this.mHandler.obtainMessage(5);
        Bundle localBundle = new Bundle();
        localBundle.putString("toast", "������������");
        localMessage.setData(localBundle);
        this.mHandler.sendMessage(localMessage);
    }

    private void connectionLost() {
        setState(1);
        Message localMessage = this.mHandler.obtainMessage(5);
        Bundle localBundle = new Bundle();
        localBundle.putString("toast", "���������������������");
        localMessage.setData(localBundle);
        this.mHandler.sendMessage(localMessage);
    }

    private void setState(int paramInt) {
        try {
            Log.d("BluetoothChatService", "setState() " + this.mState + " -> " + paramInt);
            this.mState = paramInt;
            this.mHandler.obtainMessage(1, paramInt, -1).sendToTarget();
            return;
        } finally {

        }
    }

    public void connect(BluetoothDevice paramBluetoothDevice) {
        try {
            Log.d("BluetoothChatService", "connect to: " + paramBluetoothDevice);
            if ((this.mState == 2) && (this.mConnectThread != null)) {
                this.mConnectThread.cancel();
                this.mConnectThread = null;
            }
            if (this.mConnectedThread != null) {
                this.mConnectedThread.cancel();
                this.mConnectedThread = null;
            }
            this.mConnectThread = new ConnectThread(paramBluetoothDevice);
            this.mConnectThread.start();
            setState(2);
            return;
        } finally {
        }
    }

    public void connected(BluetoothSocket paramBluetoothSocket, BluetoothDevice paramBluetoothDevice) {
        try {
            Log.d("BluetoothChatService", "connected");
            if (this.mConnectThread != null) {
                this.mConnectThread.cancel();
                this.mConnectThread = null;
            }
            if (this.mConnectedThread != null) {
                this.mConnectedThread.cancel();
                this.mConnectedThread = null;
            }
            if (this.mAcceptThread != null) {
                this.mAcceptThread.cancel();
                this.mAcceptThread = null;
            }
            this.mConnectedThread = new ConnectedThread(paramBluetoothSocket);
            this.mConnectedThread.start();
//            this.mHandler.obtainMessage(4);
//            Bundle localBundle = new Bundle();
//            localBundle.putString("device_name", paramBluetoothDevice.getName());
//            paramBluetoothSocket.setData(localBundle);
////            this.mHandler.sendMessage();
            setState(3);
            return;
        } finally {
        }
    }

    public int getState() {
        try {
            int i = this.mState;
            return i;
        } finally {

        }
    }

    public void start() {
        try {
            Log.d("BluetoothChatService", "start");
            if (this.mConnectThread != null) {
                this.mConnectThread.cancel();
                this.mConnectThread = null;
            }
            if (this.mConnectedThread != null) {
                this.mConnectedThread.cancel();
                this.mConnectedThread = null;
            }
            if (this.mAcceptThread == null) {
                this.mAcceptThread = new AcceptThread();
                this.mAcceptThread.start();
            }
            setState(1);
            return;
        } finally {
        }
    }

    public void stop() {
        try {
            Log.d("BluetoothChatService", "stop");
            if (this.mConnectThread != null) {
                this.mConnectThread.cancel();
                this.mConnectThread = null;
            }
            if (this.mConnectedThread != null) {
                this.mConnectedThread.cancel();
                this.mConnectedThread = null;
            }
            if (this.mAcceptThread != null) {
                this.mAcceptThread.cancel();
                this.mAcceptThread = null;
            }
            setState(0);
            return;
        } finally {
        }
    }

    public void write(byte[] paramArrayOfByte) {
        try {
            if (this.mState != 3) {
                return;
            }
            ConnectedThread localConnectedThread = this.mConnectedThread;
            localConnectedThread.write(paramArrayOfByte);
            return;
        } finally {
        }
    }

    private class AcceptThread extends Thread {
        private  BluetoothServerSocket mmServerSocket;
        public AcceptThread() {
            BluetoothServerSocket tmp = null;
            try {
                tmp = mAdapter.listenUsingRfcommWithServiceRecord("BluetoothCom", BluetoothChatService.MY_UUID);
                mmServerSocket = tmp;
                return;
            } catch (IOException e) {
                mmServerSocket = tmp;
            }
        }

        public void cancel() {
            Log.d("BluetoothChatService", "cancel " + this);
            try {
                this.mmServerSocket.close();
                return;
            } catch (IOException localIOException) {
                Log.e("BluetoothChatService", "close() of server failed", localIOException);
            }
        }

        public void run() {
            Log.d("BluetoothChatService", "BEGIN mAcceptThread" + this);
            setName("AcceptThread");
            BluetoothSocket socket = null;
            if (BluetoothChatService.this.mState == 3) {
                Log.i("BluetoothChatService", "END mAcceptThread");
                return;
            }
            for (;;){
                try {
                    socket = mmServerSocket.accept();
                } catch (IOException e) {

                    Log.e("BluetoothChatService", "accept() failed", e);
                }

                if(socket != null){
//                    manageConnectedSocket(socket);
                }

            }
        }


    }

    private class ConnectThread extends Thread {
        private final BluetoothDevice mmDevice;
        private final BluetoothSocket mmSocket;

        public ConnectThread(BluetoothDevice paramBluetoothDevice) {
            mmDevice = paramBluetoothDevice;
            BluetoothSocket tmp = null;
            try {
                tmp = paramBluetoothDevice.createRfcommSocketToServiceRecord(BluetoothChatService.MY_UUID);
            } catch (IOException E) {

            }
            mmSocket = tmp;

        }

        public void cancel() {
            try {
                mmSocket.close();
                return;
            } catch (IOException localIOException) {
                Log.e("BluetoothChatService", "close() of connect socket failed", localIOException);
            }
        }

        /* Error */
        public void run() {
            mAdapter.cancelDiscovery();
            try{
                mmSocket.connect();
            }catch (IOException E){
                try{
                    mmSocket.close();
                }catch (IOException error){

                }
            }
//            manageConnectedSocket(socket);
        }
    }

    private class ConnectedThread extends Thread {
        private   InputStream mmInStream;
        private  OutputStream mmOutStream;
        private final BluetoothSocket mmSocket;

        public ConnectedThread(BluetoothSocket paramBluetoothSocket) {
            Log.d("BluetoothChatService", "create ConnectedThread");
            mmSocket = paramBluetoothSocket;
            InputStream inStream = null;
            OutputStream outStream =null;
            try {
                inStream = paramBluetoothSocket.getInputStream();
                outStream = paramBluetoothSocket.getOutputStream();

            } catch (IOException e) {
                mmInStream = inStream;
                mmOutStream = outStream;

            }
        }

        public void cancel() {
            try {
                mmSocket.close();
                return;
            } catch (IOException localIOException) {
                Log.e("BluetoothChatService", "close() of connect socket failed", localIOException);
            }
        }

        public void run() {
            Log.i("BluetoothChatService", "BEGIN mConnectedThread");
            byte[] arrayOfByte1 = new byte[1024];
            int bytes;
            while (true) {
                try {
                    bytes = mmInStream.read(arrayOfByte1);
                    BluetoothChatService.this.mHandler.obtainMessage(2, bytes, -1, arrayOfByte1).sendToTarget();

                } catch (IOException localIOException) {
                    Log.e("BluetoothChatService", "disconnected", localIOException);
                    return;
                }
            }
        }

        public void write(byte[] bytes) {
            try {
                mmOutStream.write(bytes);
                BluetoothChatService.this.mHandler.obtainMessage(3, -1, -1, bytes).sendToTarget();
                return;
            } catch (IOException paramArrayOfByte) {
                Log.e("BluetoothChatService", "Exception during write", paramArrayOfByte);
            }
        }
    }
}